package lc.q851_900;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Q890 {

    public List<String> findAndReplacePattern(String[] words, String pattern) {
        List<String> results = new ArrayList<>();
        for (String word : words) {
            if (isMatch(word, pattern)) {
                results.add(word);
            }
        }
        return results;
    }

    public static void main(String[] args) {
        Q890 q890 = new Q890();
        System.out.println(q890.isMatch("ABC", "DFC"));
        System.out.println(q890.isMatch("ABB", "DFC"));
        System.out.println(q890.isMatch("ABC", "DFF"));

    }

    private boolean isMatch(String word, String pattern) {
        // 判断是否匹配
        Map<Character, Character> wordToPattern = new HashMap<>();
        Map<Character, Character> patternToWord = new HashMap<>();
        for (int i = 0; i < word.length(); i++) {
            Character wc = word.charAt(i);
            Character pc = pattern.charAt(i);
            Character patOfWord = wordToPattern.get(wc);
            if (patOfWord != null && !patOfWord.equals(pc)) {
                return false;
            }
            Character wordOfPat = patternToWord.get(pc);
            if (wordOfPat != null && !wordOfPat.equals(wc)) {
                return false;
            }
            wordToPattern.put(wc, pc);
            patternToWord.put(pc, wc);
        }
        return true;
    }

}
